package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.address.EditAddressActivity;
import com.stn.zjx.givingtreasure.domain.UserAddressDomain;
import com.stn.zjx.givingtreasure.event.RefreshWalletEvent;
import com.stn.zjx.givingtreasure.table.WalletUser;
import com.vondear.rxtools.RxActivityTool;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class LeftWalletItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<WalletUser> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public Context context;
  public int position=0;
   public LeftWalletItemListAdapter(Context context) {
       inflater = LayoutInflater.from(context);
      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_wallet_item, parent, false));
       return vh;
   }
   @SuppressLint("SetTextI18n")
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
                   IndexHolder ih= (IndexHolder) viewHolder;
                WalletUser walletUser=dataBeanList.get(i);
                 ih.tv.setText(walletUser.getName());
                 if(position==i){
                     ih.rootView.setBackgroundColor(ActivityCompat.getColor(context,R.color.light_gray));
                 }else{
                     ih.rootView.setBackgroundColor(ActivityCompat.getColor(context,R.color.white));
                 }
       ih.rootView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               position=i;
               RefreshWalletEvent event = new RefreshWalletEvent();
               event.walletUser =walletUser;
               EventBus.getDefault()
                       .post(event);
               notifyDataSetChanged();

           }
       });
   }

   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<WalletUser> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

    public void refreshList() {
        RefreshWalletEvent event = new RefreshWalletEvent();
        event.walletUser = dataBeanList.get(position);
        EventBus.getDefault()
                .post(event);
        notifyDataSetChanged();
    }


    public void appendList(List<WalletUser> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }


   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
           public TextView tv;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
            tv=itemView.findViewById(R.id.tv);

        }
   }

}